package org.example.food.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.food.pojo.Customer;
import org.example.food.pojo.User;
import org.example.food.service.AccountService;
import org.example.food.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private CustomerService customerService;
    //跳转到登录界面
    @RequestMapping("toLogin")
    public String toLogin(){
        return "index";
    }
    //跳转到注册页面
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    //跳转到主页面
    @RequestMapping("/toDashboard")
    public String toDashboard(){
        return "foodMainMenu";
    }


    //跳转到统计销售量的页面
    @RequestMapping("count")
    public String count(){
        return "chart_count";
    }
    //跳转到统计销售额的页面
    @RequestMapping("total")
    public String total(){
        return "chart_total";
    }
    //跳转到修改密码的页面
    @RequestMapping("pwd")
    public String pwd(){
        return "modify";
    }


    //注册
    @RequestMapping("/register")
    public String register(String userName,String userPwd,String confirmPwd,Model model){
        if(!userPwd.equals(confirmPwd)){
            model.addAttribute("msg","两次输入密码不一致");
            return "register";
        }else{
            Customer customer=new Customer();
            customer.setCustomerName(userName);
            String s=DigestUtil.md5Hex(userPwd);
            customer.setPassword(s);
            customerService.save(customer);
            return "index";
        }
    }


    //登录
    @RequestMapping("/login")
    public String login(String userName, String userPwd, Model model, HttpSession session){
        if(userName.equals("admin")){
            boolean b=accountService.login(userName,userPwd);
            if(b){
                QueryWrapper<User>qw=new QueryWrapper<>();
                qw.eq("username",userName);
                User one=accountService.getOne(qw);
                session.setAttribute("currentUser",userName);
                session.setAttribute("password",userPwd);
                session.setAttribute("email",one.getEmail());
                session.setAttribute("image",one.getImage());
                return "foodMainMenu";
            }else{
                model.addAttribute("msg","用户名或密码错误");
                return "index";
            }
        }else{
            boolean b=customerService.login(userName,userPwd);
            if(b){
                QueryWrapper<Customer>qw=new QueryWrapper<>();
                qw.eq("customer_name",userName);
                Customer customer=customerService.getOne(qw);
                session.setAttribute("currentUser",userName);
                session.setAttribute("password",userPwd);
                session.setAttribute("email",customer.getEmail());
                session.setAttribute("image",customer.getCimage());
                session.setAttribute("userId",customer.getId());
                return "foodMainMenu1";
            }else{
                model.addAttribute("msg","用户名或密码错误");
                return "index";
            }
        }
    }


    //修改密码
    @PostMapping("pwdUser")
    public String pwdUser(String userPwd,String newPwd,HttpSession session,Model model){
        String username= (String) session.getAttribute("currentUser");
        boolean login = accountService.login(username, userPwd);
        if(login){
            User user=new User();
            user.setUsername(username);
            String s= DigestUtil.md5Hex(newPwd);//密码加密
            user.setPassword(s);
            QueryWrapper<User>qw=new QueryWrapper<>();
            qw.eq("username",user.getUsername());
            boolean update = accountService.update(user, qw);
            if(update){
                return "index";
            }else{
                model.addAttribute("loginFail","修改密码失败");
            }
        }else{
            model.addAttribute("loginFail","用户验证失败");

        }
        return "modify";
    }
    //退出
    @RequestMapping("logout")
    public String logout(){
        return "index";
    }

    //跳转到个人设置页面
    @RequestMapping("profile")
    public String profile(HttpSession session,Model model){
        String currentUser= (String) session.getAttribute("currentUser");
        String password= (String) session.getAttribute("password");
        if(currentUser.equals("admin")){
            QueryWrapper<User>qw=new QueryWrapper<>();
            qw.eq("username",currentUser);
            User user=accountService.getOne(qw);
            user.setPassword(password);
            model.addAttribute("user",user);
            return "profile-admin";
        }else{
            QueryWrapper<Customer>qw=new QueryWrapper<>();
            qw.eq("customer_name",currentUser);
            Customer customer=customerService.getOne(qw);
            customer.setPassword(password);
            model.addAttribute("user",customer);
            return "profile-customer";
        }
    }
    //修改管理员个人信息
    @RequestMapping("updateAdminProfile")
    public String updateAdminProfile(User user){
        //修改数据库
        user.setPassword(DigestUtil.md5Hex(user.getPassword()));
        boolean b=accountService.updateById(user);
        return "index";
    }
    //修改顾客个人信息
    @RequestMapping("updateCustomerProfile")
    public String updateCustomerProfile(Customer customer){
        customer.setPassword(DigestUtil.md5Hex(customer.getPassword()));
        boolean b=customerService.updateById(customer);
        return "index";
    }
}
